<template>
  <div style="width: 100%">
    <div id="deawer">
      <v-tableTitle :btnConfig="btnConfig" :title="$t('out.outbound')" @cancel="cancel" @save="save"
        @allocation="allocation" @grant="grant" @revocation="revocation" @order="order" @suspend="suspend"
        @deliver="deliver"></v-tableTitle>
      <div class="detailBox" style="height:320px">
        <v-detail ref="detail"></v-detail>
      </div>
      <div class="detailListBox">
        <v-detailList ref="detailList"></v-detailList>
      </div>
    </div>
  </div>

</template>

<script>
import baseMixin from "@/mixins/BaseMixin";
import tableTitle from "commons/TableTitle";
import detail from "../details/Detail"
import detailList from "../detailsList/DetailList"
import config from "../Config";

export default {
  name: "",
  mixins: [baseMixin, config],
  data() {
    return {
      newFlag: false,
      routeData: {},
    };
  },
  computed: {
    btnConfig() {
      if (this.newFlag) {
        return [
          { label: this.$t('common.save'), certigier: !this.btnPermNameList.includes('WM020116'), invalid: "save" },
          { label: this.$t('common.return'), certigier: !this.btnPermNameList.includes('WM020117'), invalid: "cancel" },
        ]
      } else {
        return [
          { label: this.$t('common.save'), certigier: !this.btnPermNameList.includes('WM020116'), invalid: "save" },
          {
            type: "more", label: this.$t('common.moreMenu'), certigier: !this.btnPermNameList.includes('WM020118'), invalid: "down", btnConfig: [
              { label: this.$t('out.allocation'), certigier: !this.btnPermNameList.includes('WM020119'), invalid: "allocation" },
              { label: this.$t('out.grant'), certigier: !this.btnPermNameList.includes('WM020120'), invalid: "grant" },
              { label: this.$t('out.revocation'), certigier: !this.btnPermNameList.includes('WM020121'), invalid: "revocation" },
              { label: this.$t('out.allPick'), certigier: !this.btnPermNameList.includes('WM020122'), invalid: "order" },
              { label: this.$t('out.allShip'), certigier: !this.btnPermNameList.includes('WM020123'), invalid: "deliver" },
              { label: this.$t('out.suspend'), certigier: !this.btnPermNameList.includes('WM020124'), invalid: "suspend" },
            ]
          },
          { label: this.$t('common.return'), certigier: !this.btnPermNameList.includes('WM020117'), invalid: "cancel" },
        ]
      }

    }
  },
  created() {
  },
  methods: {
    suspend(data) {
      this.$confirm('是否暂停出货订单', '提示', {
        confirmButtonText: this.$t('common.confirm'),
        cancelButtonText: this.$t('common.cancel'),
        type: 'warning'
      }).then(() => {
        this.$api.post(`/suspendByOrderkey/${this.routeData.orderkey}/${this.$store.state.wms}`).then(res => {
          
            this.$refs.detail.$refs.comRef.setsuspendedindicator()
        })
      }).catch(() => {
      });
    },
    async deliver() {
      try {
        let res = await this.$api.get(`/ordersDeliverAll/${this.routeData.orderkey}`);
        
          this.$message.success(res.returnMgr);
        
      } catch (e) {
        this.$message.error(e);
      }
    },
    async order() {
      try {
        let res = await this.$api.get(`/ordersPickingAll/${this.routeData.orderkey}`);
        
          this.$message.success(res.returnMgr);
        
      } catch (e) {
        this.$message.error(e);
      }
    },
    async revocation() {
      try {
        let res = await this.$api.get(`/ordersRevokeAllocate/${this.routeData.orderkey}`);
        
          this.$message.success(res.returnMgr);
        
      } catch (e) {
        this.$message.error(e);
      }
    },
    async allocation() {
      try {
        let res = await this.$api.post(`/allocateByOrderkey/${this.routeData.orderkey}/${this.$store.state.wms}`);
        
          this.$message.success(res.returnMgr);
        
      } catch (e) {
        this.$message.error(e);
      }
    },
    async grant() {
      try {
        let res = await this.$api.get(`/ordersReleaseTask/${this.routeData.orderkey}`);
        
          this.$message.success(res.returnMgr);
        
      } catch (e) {
        this.$message.error(e);
      }
    },
    //保存按钮
    async save() {
      let reg = /^(\-|\+)?\d+(\.\d+)?$/;//正数、负数、和小数
      let detail = this.$refs.detail.detailData;
      if (detail.stop != null && detail.stop != '' && !reg.test(detail.stop)) {
        this.$message.error(this.$t('msg.msg62'))
        return
      }
      let detailList = this.toEmpty(this.$refs.detailList.detailData);
      let data = {};
      if (this.newFlag) {
        data = {
          ...detail,
          orderdetail: [{ ...detailList }]
        }
      } else {
        if (this.$store.state.temporary) {
          data = {
            ...detail,
            orderdetail: [{ ...detailList }]
          }
        } else {
          data = {
            ...detail
          }
        }
      }
      try {
        let res = await this.$api.post('/excuteOrders', data);
        
          this.$message.success(res.returnMgr);
          this.$store.commit("setDetailsNew", false);
          this.cancel();
        
      } catch (e) {
        this.$message.error(e)
      }
    },
    //弹框取消
    cancel() {
      let routerName = JSON.parse(sessionStorage.getItem('routerName'));
      let num = -1;
      if (routerName) {
        num = routerName.indexOf("orders");
      }
      if (num === -1) {
        this.$router.push({ name: "orders" });
        this.$store.commit("setDetailsNew", false);
      } else {
        num = num - 0;
        routerName.splice(num, 1);
        sessionStorage.setItem('routerName', JSON.stringify(routerName));
        this.$router.push({ name: "home1" })
      }

    },
  },
  mounted() {
    // if (Object.keys(this.$store.state.skip).length !== 0) {
    //   let data = this.$store.state.skip;
    //   this.routeData = data.routeData;
    //   this.newFlag = data.newFlag;
    // } else if (this.$store.state.newFlag) {
    //   this.newFlag = true;
    // } else {
    //   this.routeData = this.$store.state.routeData
    // }
    const query = this.$route.query;
    if(query.newFlag){
      this.newFlag = true;
    }else{
      this.newFlag = false;
      this.routeData = query;
    }
  },
  watch: {},
  components: {
    "v-tableTitle": tableTitle,
    "v-detail": detail,
    "v-detailList": detailList,
  },
  beforeDestroy() {
    let routerName = JSON.parse(sessionStorage.getItem('routerName'));
    let num = -1;
    if (routerName) {
      num = routerName.indexOf("orders");
    }
    if (num === -1) {
    } else {
      num = num - 0;
      routerName.splice(num, 1);
      sessionStorage.setItem('routerName', JSON.stringify(routerName));
    }
  }
}
</script>

<style scoped>

</style>
